
//toast提示信息
export function showToast (title, icon = 'none') {
  uni.showToast({
    title: title.toString(),
    icon: icon
  });
};



export function formatSeconds (value) {
  let result = parseInt(value)
  let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
  let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));

  return `${h}:${m}:${s}`;
};


var keyStr = "ABCDEFGHIJKLMNOP" + "QRSTUVWXYZabcdef" + "ghijklmnopqrstuv"
  + "wxyz0123456789+/" + "=";

export function encode64 (input) {

  var output = "";
  var chr1, chr2, chr3 = "";
  var enc1, enc2, enc3, enc4 = "";
  var i = 0;
  do {
    chr1 = input.charCodeAt(i++);
    chr2 = input.charCodeAt(i++);
    chr3 = input.charCodeAt(i++);
    enc1 = chr1 >> 2;
    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
    enc4 = chr3 & 63;
    if (isNaN(chr2)) {
      enc3 = enc4 = 64;
    } else if (isNaN(chr3)) {
      enc4 = 64;
    }
    output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)
      + keyStr.charAt(enc3) + keyStr.charAt(enc4);
    chr1 = chr2 = chr3 = "";
    enc1 = enc2 = enc3 = enc4 = "";
  } while (i < input.length);

  return output;
}


// 防止多次重复点击
// data 中 noClick:true,
// 当前函数配置为公共方法 Vue.prototype.$noMultipleClicks = noMultipleClicks
// 使用方式 @click="$noMultipleClicks(commitWork)"

export function noMultipleClicks (methods, info) {
  // methods是需要点击后需要执行的函数， info是点击需要传的参数
  let that = this;
  if (that.noClick) {
    // 第一次点击
    that.noClick = false;
    if (info && info !== '') {
      // info是执行函数需要传的参数
      methods(info);
    } else {
      methods();
    }
    setTimeout(() => {
      that.noClick = true;
    }, 1000)
  } else {
    // 这里是重复点击的判断
  }
}

export function svgToBase64(svg) {
    // 将SVG转换为UTF-8编码的字符串
    const utf8SVG = encodeURIComponent(svg).replace(/%([0-9A-F]{2})/g, function(match, p1) {
        return String.fromCharCode('0x' + p1);
    });
    
    // 将UTF-8编码的字符串转换为Byte String
    const byteString = utf8ToByteString(utf8SVG);
 
    // 将Byte String转换为Base64编码
    return btoa(byteString);
}
 
function utf8ToByteString(str) {
    // 将字符串转换为字符数组
    const charList = str.split('');
    // 创建一个字节数组
    const byteArray = [];
 
    // 将每个字符转换为其对应的字节
    charList.forEach(function(c) {
        byteArray.push(c.charCodeAt(0) & 0xff);
    });
 
    // 将字节数组转换为字符串
    return String.fromCharCode.apply(null, byteArray);
}